package kjmvc;

import java.lang.reflect.*;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.sql.ResultSet;
//import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import kjmvc.util.KJException;
public class KJBean {
	public KJBean() {
	}
	
	public KJBean(HttpServletRequest request){
		setBean(this, request);
	}
	
	public void setBean(HttpServletRequest request) {
		setBean(this, request);
	}
	
	public static void setBean(Object bean, HttpServletRequest request){
		Class<?> beanClass = bean.getClass();
		Method [] beanMethod = beanClass.getDeclaredMethods();
		for (Method me : beanMethod) {
			if(me.getName().startsWith("set")) {
				String param = request.getParameter(me.getName().substring(3).toLowerCase());
				Class [] paramTypes = me.getParameterTypes();

				if(paramTypes.length != 1 || param == null || param.length()<1)
					continue;
				
				try {
					Object params[] = null;
					if( paramTypes[0] == String.class )
						params = new Object[]{new String(param)};
					else if(paramTypes[0] == short.class)
						params = new Object[]{new Short(param)};
					else if(paramTypes[0] == int.class)
						params = new Object[]{new Integer(param)};
					else if(paramTypes[0] == long.class)
						params = new Object[]{new Long(param)};
					else if(paramTypes[0] == float.class)
						params = new Object[]{new Float(param)};
					else if(paramTypes[0] == double.class)
						params = new Object[]{new Double(param)};
					else if(paramTypes[0] == boolean.class)
						params = new Object[]{new Boolean(param)};
					else if(paramTypes[0] == byte.class)
						params = new Object[]{new Byte(param)};
					else params = null;
				
					if( params != null)
						me.invoke(bean, params);
				} catch (NumberFormatException e) {
					System.out.println("NumberFormatException occurs");
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				System.out.println("method:"+me.getName());
			}
		}
	}
	
	public static KJBean setBeanFromRS(ResultSet rs, Object bean) {
		Class<?> beanClass = bean.getClass();
		Method [] beanMethod = beanClass.getDeclaredMethods();
		for (Method me : beanMethod) {
			if(me.getName().startsWith("set")) {
				Class [] paramTypes = me.getParameterTypes();
				String field = me.getName().substring(3).toUpperCase();
				if(paramTypes.length != 1)
					continue;
				
				try {
					Object params[] = null;
					if( paramTypes[0] == String.class )
							params = new Object[]{rs.getString(field)};
					else if(paramTypes[0] == short.class)
						params = new Object[]{new Short(rs.getShort(field))};
					else if(paramTypes[0] == int.class)
						params = new Object[]{new Integer(rs.getInt(field))};
					else if(paramTypes[0] == long.class)
						params = new Object[]{new Long(rs.getLong(field))};
					else if(paramTypes[0] == float.class)
						params = new Object[]{new Float(rs.getFloat(field))};
					else if(paramTypes[0] == double.class)
						params = new Object[]{new Double(rs.getDouble(field))};
					else if(paramTypes[0] == boolean.class)
						params = new Object[]{new Boolean(rs.getBoolean(field))};
					else if(paramTypes[0] == byte.class)
						params = new Object[]{new Byte(rs.getByte(field))};
					else params = null;
				
					if( params != null)
						me.invoke(bean, params);
				} catch (NumberFormatException e) {
					System.out.println("NumberFormatException occurs");
				} catch (SQLException e) {
					System.out.println("SQLException occurs");
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				System.out.println("method:"+me.getName());
			}
		}
		return (KJBean) bean;
	}

}